<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Materia extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url', 'form'));
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Pensum_Model');
        $this->load->model('Carrera_Model');
        $this->load->model('Ciclos_Model');
        $this->load->model('Materia_Model');
    }

    public function index() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
    }

    public function inicio() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $ide_car = $this->uri->segment(3);
        $ide_pen = $this->uri->segment(4);
        $codigo_cic = $this->uri->segment(5);



        if (!empty($ide_car) && $ide_car != -1) {
            $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);
            $this->data['ide_car'] = $ide_car;
            //$this->data['ide_pen'] = -1;
        } else {
            $this->data['ide_car'] = -1;
        }
        if (!empty($ide_pen) && $ide_pen != -1) {
            $this->data['ide_pen'] = $ide_pen;
            $this->data['ciclo'] = $this->Ciclos_Model->getCicloPensum($ide_pen);
        } else {
            $this->data['ide_pen'] = -1;
        }
        if (!empty($codigo_cic) && $codigo_cic != -1) {
            $this->data['materia'] = $this->Materia_Model->getListado($codigo_cic);
            $this->data['codigo_cic'] = $codigo_cic;
        } else {
            $this->data['codigo_cic'] = -1;
        }
        
        //$this->data['materia'] = $this->Materia_Model->getListado($codigo_cic);
        $this->_render('materia/start');
    }

    public function crear() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->library('form_validation');

        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $ide_car = $this->uri->segment(3);
        $ide_pen = $this->uri->segment(4);

        if (!empty($ide_car) && $ide_car != -1) {
            $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);
            $this->data['ide_car'] = $ide_car;
            //$this->data['ide_pen'] = -1;
        } else {
            $this->data['ide_car'] = -1;
        }
        if (!empty($ide_pen) && $ide_pen != -1) {
            $this->data['ciclo'] = $this->Ciclos_Model->getCiclosPensum($ide_pen);
            $this->data['requisitos'] = $this->Materia_Model->getPresequisitos($ide_car, $ide_pen);
            $this->data['ide_pen'] = $ide_pen;
        } else {
            $this->data['ide_pen'] = -1;
        }


        $this->_render('materia/new');

        //redirect('auth', 'refresh');
    }

    public function actualizar() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
        $this->data['carrera'] = $this->Carrera_Model->getListado();

        $opc = $this->uri->segment(3);
        $ide_car = $this->uri->segment(4);
        $ide_mat = $this->uri->segment(5);
        $ide_pen = $this->uri->segment(6);
        $ide_cic = $this->uri->segment(7);


        if (!isset($_POST['codigo_mat'])) {
            $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);

            if (!empty($ide_pen) && $ide_pen != -1) {
                $this->data['ciclo'] = $this->Ciclos_Model->getCiclosPensum($ide_pen);
                $this->data['requisitos'] = $this->Materia_Model->getPresequisitos($ide_car, $ide_pen);

                $this->data['ide_pen'] = $ide_pen;
                //print_r($this->data['ciclo']);
            } else {
                $this->data['ide_pen'] = -1;
            }
            if (!empty($ide_cic) && $ide_cic != -1) {
                $this->data['ide_cic'] = $ide_cic;
            } else {
                $this->data['ide_cic'] = -1;
            }

            $this->data['materia'] = $this->Materia_Model->getDatosMateria($ide_mat);
            $this->data['ide_car'] = $ide_car;
            $this->data['ide_mat'] = $ide_mat;
            $this->data['opc'] = $opc;
            $this->_render('materia/update');
        } else {
            $ide_car = $this->input->post('carrera');
            $ide_pen = $this->input->post('pensum');
            $ide_cic = $this->input->post('ciclo');
            $ide_mat = $this->input->post('ide_mat');
            $opc = $this->input->post('txtopc');
            $this->data['ide_car'] = $ide_car;
            $this->data['ide_pen'] = $ide_pen;
            $this->data['ide_cic'] = $ide_cic;
            $this->data['ide_mat'] = $ide_mat;
            $this->data['opc'] = $opc;
            $this->data['materia'] = $this->Materia_Model->getDatosMateria($ide_mat);

            If ($ide_pen == -1 || $ide_cic == -1) {
                $this->data['msj_error1'] = "Error debe seleccionar el Pensum o  el Ciclo";
                //redirect('materia/actualizar/' . $opc . '/' . $ide_car . '/' . $ide_mat);
                $this->_render('materia/update');
            } else {


                $this->form_validation->set_rules('codigo_mat', 'Codigo de la Materia', 'required|trim');
                $this->form_validation->set_rules('nombre_mat', 'Nombre de la Materia', 'required|trim');


                if (($this->form_validation->run() == FALSE)) {
                    $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);
                    $this->data['ciclo'] = $this->Ciclos_Model->getCiclosPensum($ide_pen);
                    $this->data['requisitos'] = $this->Materia_Model->getPresequisitos($ide_car, $ide_pen);



                    $this->_render('materia/update');
                    //redirect("institucion/actualizar/".$opc."/".$ide);
                } else {

                    $materia = array(
                        "ejeform_mat" => $this->input->post('ejeform_mat'),
                        "campocien_mat" => $this->input->post('campocien_mat'),
                        "codigo_mat" => $this->input->post('codigo_mat'),
                        "nombre_mat" => $this->input->post('nombre_mat'),
                        "creditos_mat" => $this->input->post('creditos_mat'),
                        "requisito1_mat" => $this->input->post('requisito1_mat'),
                        "requisito2_mat" => $this->input->post('requisito2_mat'),
                        "requisito3_mat" => $this->input->post('requisito3_mat'),
                        "requisito4_mat" => $this->input->post('requisito4_mat'),
                        "tipomat_mat" => $this->input->post('tipomat_mat'),
                        "ciclos_ide_cic" => $this->input->post('ciclo'));


                    //print_r($materia);
                    if ($opc == 'upd')
                        $this->Materia_Model->actualizarMateria($materia, $ide_mat);
                    else
                        $this->Materia_Model->eliminarMateria($ide_mat);
                    redirect('materia/inicio');
                }
            }
        }
    }

    function presearch() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $who = $this->input->post('txtsearch');


        if (empty($who))
            redirect("materia/inicio");
        else {
            // echo $who." busqueda";
            $this->data['materia'] = $this->Materia_Model->getBusquedaMateria($who);
            $this->_render('materia/start');
        }
    }

    public function ingresodatos() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        

        $this->load->library('form_validation');
        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $ide_car = $this->input->post('carrera');
        if ($ide_car == -1) {
            $this->data['ide_car'] = -1;
            $this->data['msj_error'] = "Debe seleccionar una Carrera";
            $this->_render('materia/new');
        } else {
            $ide_pen = $this->input->post('pensum');
            if ($ide_pen == -1) {
                $this->data['ide_car'] = $ide_car;
                $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);
                $this->data['ide_pen'] = -1;
                $this->data['msj_error1'] = "Debe seleccionar un Pensum";
                $this->_render('materia/new');
            } else {

                $this->form_validation->set_rules('codigo_mat', 'Codigo de la Materia', 'required|trim');
                $this->form_validation->set_rules('nombre_mat', 'Nombre de la Materia', 'required|trim');
                $this->data['ide_car'] = $ide_car;
                $this->data['ide_pen'] = $ide_pen;

                if (($this->form_validation->run() == FALSE)) {
                    $this->data['pensum'] = $this->Pensum_Model->getPensumCarreras($ide_car);
                    $this->data['ciclo'] = $this->Ciclos_Model->getCiclosPensum($ide_pen);

                    $this->_render('materia/new');
                } else {

                    $materia = array(
                        "ejeform_mat" => $this->input->post('ejeform_mat'),
                        "campocien_mat" => $this->input->post('campocien_mat'),
                        "codigo_mat" => $this->input->post('codigo_mat'),
                        "nombre_mat" => $this->input->post('nombre_mat'),
                        "creditos_mat" => $this->input->post('creditos_mat'),
                        "requisito1_mat" => $this->input->post('requisito1_mat'),
                        "requisito2_mat" => $this->input->post('requisito2_mat'),
                        "requisito3_mat" => $this->input->post('requisito3_mat'),
                        "requisito4_mat" => $this->input->post('requisito4_mat'),
                        "tipomat_mat" => $this->input->post('tipomat_mat'),
                        "ciclos_ide_cic" => $this->input->post('ciclo'));

                    $this->Materia_Model->grabarMateria($materia);

                    redirect('materia/inicio');
                }
            }
        }
    }

}

/* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */


    